
#include <pic.h> 
#include "my_macros.h" 
#include "Inputs.h" 
#include "SysTime.h"

#define   DEBOUNCE			2	  /* how many times check the key every 10mS*/

INPUTS_STATE InputsState;
unsigned char InputsEvent;

/* Debounce counters */
unsigned char d1 = 0;
unsigned char d2 = 0;

/* unsigned char d3 = 0; */
/* unsigned char d4 = 0; */
/* unsigned char d5 = 0; */

#define   LOW 	0
#define   HIGH	(!LOW)

#define   PIN_INP_1		RA0
#define   PIN_INP_2		RA1	
/* #define   PIN_INP_3		RA1 */
/* #define   PIN_INP_4		RA2 */
/* #define   PIN_INP_5		RA3 */


void Inputs_Init( void )
{
	TRISA |= 0b00000011;
	InputsEvent = 0;
}

void Drive_Inputs ( void )
{
	InputsEvent = 0;
		
	if ( Time_10ms == FALSE )
		return; 
	
   /* Input 1 */
	if ( InputsState.INP_1 == LOW )
	{
		if ( PIN_INP_1 )
		{
			if ( ++d1 > DEBOUNCE )
			{
				d1 = 0;
				InputsState.INP_1 = HIGH;
			}
		}
		else
			d1 = 0;
	}
	else
	{
		if ( !PIN_INP_1 )
		{
			if ( ++d1 > DEBOUNCE )
			{
				d1 = 0;
				InputsState.INP_1 = LOW;
				InputsEvent = INPUT_1;
				return;
			}
		}
		else
			d1 = 0;
	}

	/* Input 2 */
	if ( InputsState.INP_2 == LOW )
	{
		if ( PIN_INP_2 )
		{
			if ( ++d2 > DEBOUNCE )
			{
				d2 = 0;
				InputsState.INP_2 = HIGH;
			}
		}
		else
			d2 = 0;
	}
	else
	{
		if ( !PIN_INP_2 )
		{
			if ( ++d2 > DEBOUNCE )
			{
				d2 = 0;
				InputsState.INP_2 = LOW;
				InputsEvent = INPUT_2;
				return;
			}
		}
		else
			d2 = 0;
	}
}







/* 	/\* Input 3 *\/ */
/* 	if ( InputsState.INP_3 == LOW ) */
/* 	{ */
/* 		if ( PIN_INP_3 ) */
/* 		{ */
/* 			if ( ++d3 > DEBOUNCE ) */
/* 			{ */
/* 				d3 = 0; */
/* 				InputsState.INP_3 = HIGH; */
/* 				//InputsEvent = INPUT_3_UP; */
/* //				InputsEvent = INP_3_UP; */
/* 			} */
/* 		} */
/* 		else */
/* 			d3 = 0; */
/* 	} */
/* 	else */
/* 	{ */
/* 		if ( !PIN_INP_3 ) */
/* 		{ */
/* 			if ( ++d3 > DEBOUNCE ) */
/* 			{ */
/* 				d3 = 0; */
/* 				InputsState.INP_3 = LOW; */
/* 				InputsEvent = INPUT_3_DOWN; */
/* 				//InputsEvent = INP_3_DOWN; */
/* 			} */
/* 		} */
/* 		else */
/* 			d3 = 0; */
/* 	} */
	
/* 	/\* Input 4 *\/ */
/* 	if ( InputsState.INP_4 == LOW ) */
/* 	{ */
/* 		if ( PIN_INP_4 ) */
/* 		{ */
/* 			if ( ++d4 > DEBOUNCE ) */
/* 			{ */
/* 				d4 = 0; */
/* 				InputsState.INP_4 = HIGH; */
/* 				//InputsEvent = INPUT_4_UP; */
/* 				//InputsEvent = INP_4_UP; */
/* 			} */
/* 		} */
/* 		else */
/* 			d4 = 0; */
/* 	} */
/* 	else */
/* 	{ */
/* 		if ( !PIN_INP_4 ) */
/* 		{ */
/* 			if ( ++d4 > DEBOUNCE ) */
/* 			{ */
/* 				d4 = 0; */
/* 				InputsState.INP_4 = LOW; */
/* 				InputsEvent = INPUT_4_DOWN; */
/* 				//InputsEvent = INP_4_DOWN; */
/* 			} */
/* 		} */
/* 		else */
/* 			d4 = 0; */
/* 	} */

	
/* 	/\* Input 5 *\/ */
/* 	if ( InputsState.INP_5 == LOW ) */
/* 	{ */
/* 		if ( PIN_INP_5 ) */
/* 		{ */
/* 			if ( d5 > DEBOUNCE ) */
/* 			{ */
/* 				d5 = 0; */
/* 				InputsState.INP_5 = HIGH; */
/* 				//InputsEvent = INPUT_5_UP; */
/* 				//InputsEvent = INP_0_UP; */
/* 			} */
/* 			else  */
/* 				d5++; */
/* 		} */
/* 		else */
/* 			d5 = 0; */
/* 	} */
/* 	else */
/* 	{ */
/* 		if ( !PIN_INP_5 ) */
/* 		{ */
/* 			if ( d5 > DEBOUNCE ) */
/* 			{ */
/* 				d5 = 0; */
/* 				InputsState.INP_5 = LOW; */
/* 				InputsEvent = INPUT_5_DOWN; */
/* 				//InputsEvent = INP_0_DOWN; */
/* 			} */
/* 			else */
/* 				d5++; */
/* 		} */
/* 		else */
/* 			d5 = 0; */
/* 	} */
